<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html lang="en">

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>LCOV - Lua 5.1.5 - src/lauxlib.c</title>
  <link rel="stylesheet" type="text/css" href="../gcov.css">
</head>

<body>

  <table width="100%" border=0 cellspacing=0 cellpadding=0>
    <tr><td class="title">LCOV - code coverage report</td></tr>
    <tr><td class="ruler"><img src="../glass.png" width=3 height=3 alt=""></td></tr>

    <tr>
      <td width="100%">
        <table cellpadding=1 border=0 width="100%">
          <tr>
            <td width="10%" class="headerItem">Current view:</td>
            <td width="35%" class="headerValue"><a href="../index.html">top level</a> - <a href="index.html">src</a> - lauxlib.c</td>
            <td width="5%"></td>
            <td width="15%"></td>
            <td width="10%" class="headerCovTableHead">Hit</td>
            <td width="10%" class="headerCovTableHead">Total</td>
            <td width="15%" class="headerCovTableHead">Coverage</td>
          </tr>
          <tr>
            <td class="headerItem">Test:</td>
            <td class="headerValue">Lua 5.1.5</td>
            <td></td>
            <td class="headerItem">Lines:</td>
            <td class="headerCovTableEntry">261</td>
            <td class="headerCovTableEntry">309</td>
            <td class="headerCovTableEntryMed">84.5 %</td>
          </tr>
          <tr>
            <td class="headerItem">Date:</td>
            <td class="headerValue">2019-06-13 19:44:15</td>
            <td></td>
          </tr>
          <tr>
            <td class="headerItem">Legend:</td>
            <td class="headerValueLeg">            Lines:
            <span class="coverLegendCov">hit</span>
            <span class="coverLegendNoCov">not hit</span>
</td>
            <td></td>
          </tr>
          <tr><td><img src="../glass.png" width=3 height=3 alt=""></td></tr>
        </table>
      </td>
    </tr>

    <tr><td class="ruler"><img src="../glass.png" width=3 height=3 alt=""></td></tr>
  </table>

  <table cellpadding=0 cellspacing=0 border=0>
    <tr>
      <td><br></td>
    </tr>
    <tr>
      <td>
<pre class="sourceHeading">          Line data    Source code</pre>
<pre class="source">
<span class="lineNum">       1 </span>            : /*
<span class="lineNum">       2 </span>            : ** $Id: lauxlib.c,v 1.159.1.3 2008/01/21 13:20:51 roberto Exp $
<span class="lineNum">       3 </span>            : ** Auxiliary functions for building Lua libraries
<span class="lineNum">       4 </span>            : ** See Copyright Notice in lua.h
<span class="lineNum">       5 </span>            : */
<span class="lineNum">       6 </span>            : 
<span class="lineNum">       7 </span>            : 
<span class="lineNum">       8 </span>            : #include &lt;ctype.h&gt;
<span class="lineNum">       9 </span>            : #include &lt;errno.h&gt;
<span class="lineNum">      10 </span>            : #include &lt;stdarg.h&gt;
<span class="lineNum">      11 </span>            : #include &lt;stdio.h&gt;
<span class="lineNum">      12 </span>            : #include &lt;stdlib.h&gt;
<span class="lineNum">      13 </span>            : #include &lt;string.h&gt;
<span class="lineNum">      14 </span>            : 
<span class="lineNum">      15 </span>            : 
<span class="lineNum">      16 </span>            : /* This file uses only the official API of Lua.
<span class="lineNum">      17 </span>            : ** Any function declared here could be written as an application function.
<span class="lineNum">      18 </span>            : */
<span class="lineNum">      19 </span>            : 
<span class="lineNum">      20 </span>            : #define lauxlib_c
<span class="lineNum">      21 </span>            : #define LUA_LIB
<span class="lineNum">      22 </span>            : 
<span class="lineNum">      23 </span>            : #include &quot;lua.h&quot;
<span class="lineNum">      24 </span>            : 
<span class="lineNum">      25 </span>            : #include &quot;lauxlib.h&quot;
<span class="lineNum">      26 </span>            : 
<span class="lineNum">      27 </span>            : 
<span class="lineNum">      28 </span>            : #define FREELIST_REF    0       /* free list of references */
<span class="lineNum">      29 </span>            : 
<span class="lineNum">      30 </span>            : 
<span class="lineNum">      31 </span>            : /* convert a stack index to positive */
<span class="lineNum">      32 </span>            : #define abs_index(L, i)         ((i) &gt; 0 || (i) &lt;= LUA_REGISTRYINDEX ? (i) : \
<span class="lineNum">      33 </span>            :                                         lua_gettop(L) + (i) + 1)
<span class="lineNum">      34 </span>            : 
<span class="lineNum">      35 </span>            : 
<span class="lineNum">      36 </span>            : /*
<span class="lineNum">      37 </span>            : ** {======================================================
<span class="lineNum">      38 </span>            : ** Error-report functions
<span class="lineNum">      39 </span>            : ** =======================================================
<span class="lineNum">      40 </span>            : */
<span class="lineNum">      41 </span>            : 
<span class="lineNum">      42 </span>            : 
<span class="lineNum">      43 </span><span class="lineCov">         41 : LUALIB_API int luaL_argerror (lua_State *L, int narg, const char *extramsg) {</span>
<span class="lineNum">      44 </span>            :   lua_Debug ar;
<span class="lineNum">      45 </span><span class="lineCov">         41 :   if (!lua_getstack(L, 0, &amp;ar))  /* no stack frame? */</span>
<span class="lineNum">      46 </span><span class="lineNoCov">          0 :     return luaL_error(L, &quot;bad argument #%d (%s)&quot;, narg, extramsg);</span>
<span class="lineNum">      47 </span><span class="lineCov">         41 :   lua_getinfo(L, &quot;n&quot;, &amp;ar);</span>
<span class="lineNum">      48 </span><span class="lineCov">         41 :   if (strcmp(ar.namewhat, &quot;method&quot;) == 0) {</span>
<span class="lineNum">      49 </span><span class="lineCov">          2 :     narg--;  /* do not count `self' */</span>
<span class="lineNum">      50 </span><span class="lineCov">          2 :     if (narg == 0)  /* error is in the self argument itself? */</span>
<span class="lineNum">      51 </span><span class="lineNoCov">          0 :       return luaL_error(L, &quot;calling &quot; LUA_QS &quot; on bad self (%s)&quot;,</span>
<span class="lineNum">      52 </span>            :                            ar.name, extramsg);
<span class="lineNum">      53 </span>            :   }
<span class="lineNum">      54 </span><span class="lineCov">         41 :   if (ar.name == NULL)</span>
<span class="lineNum">      55 </span><span class="lineCov">          2 :     ar.name = &quot;?&quot;;</span>
<span class="lineNum">      56 </span><span class="lineCov">         41 :   return luaL_error(L, &quot;bad argument #%d to &quot; LUA_QS &quot; (%s)&quot;,</span>
<span class="lineNum">      57 </span>            :                         narg, ar.name, extramsg);
<span class="lineNum">      58 </span>            : }
<span class="lineNum">      59 </span>            : 
<span class="lineNum">      60 </span>            : 
<span class="lineNum">      61 </span><span class="lineCov">          8 : LUALIB_API int luaL_typerror (lua_State *L, int narg, const char *tname) {</span>
<span class="lineNum">      62 </span><span class="lineCov">          8 :   const char *msg = lua_pushfstring(L, &quot;%s expected, got %s&quot;,</span>
<span class="lineNum">      63 </span>            :                                     tname, luaL_typename(L, narg));
<span class="lineNum">      64 </span><span class="lineCov">          8 :   return luaL_argerror(L, narg, msg);</span>
<span class="lineNum">      65 </span>            : }
<span class="lineNum">      66 </span>            : 
<span class="lineNum">      67 </span>            : 
<span class="lineNum">      68 </span><span class="lineCov">          8 : static void tag_error (lua_State *L, int narg, int tag) {</span>
<span class="lineNum">      69 </span><span class="lineCov">          8 :   luaL_typerror(L, narg, lua_typename(L, tag));</span>
<span class="lineNum">      70 </span><span class="lineNoCov">          0 : }</span>
<span class="lineNum">      71 </span>            : 
<span class="lineNum">      72 </span>            : 
<span class="lineNum">      73 </span><span class="lineCov">        137 : LUALIB_API void luaL_where (lua_State *L, int level) {</span>
<span class="lineNum">      74 </span>            :   lua_Debug ar;
<span class="lineNum">      75 </span><span class="lineCov">        137 :   if (lua_getstack(L, level, &amp;ar)) {  /* check function at level */</span>
<span class="lineNum">      76 </span><span class="lineCov">        137 :     lua_getinfo(L, &quot;Sl&quot;, &amp;ar);  /* get info about it */</span>
<span class="lineNum">      77 </span><span class="lineCov">        137 :     if (ar.currentline &gt; 0) {  /* is there info? */</span>
<span class="lineNum">      78 </span><span class="lineCov">         86 :       lua_pushfstring(L, &quot;%s:%d: &quot;, ar.short_src, ar.currentline);</span>
<span class="lineNum">      79 </span><span class="lineCov">         86 :       return;</span>
<span class="lineNum">      80 </span>            :     }
<span class="lineNum">      81 </span>            :   }
<span class="lineNum">      82 </span><span class="lineCov">         51 :   lua_pushliteral(L, &quot;&quot;);  /* else, no information available... */</span>
<span class="lineNum">      83 </span>            : }
<span class="lineNum">      84 </span>            : 
<span class="lineNum">      85 </span>            : 
<span class="lineNum">      86 </span><span class="lineCov">        130 : LUALIB_API int luaL_error (lua_State *L, const char *fmt, ...) {</span>
<span class="lineNum">      87 </span>            :   va_list argp;
<span class="lineNum">      88 </span><span class="lineCov">        130 :   va_start(argp, fmt);</span>
<span class="lineNum">      89 </span><span class="lineCov">        130 :   luaL_where(L, 1);</span>
<span class="lineNum">      90 </span><span class="lineCov">        130 :   lua_pushvfstring(L, fmt, argp);</span>
<span class="lineNum">      91 </span><span class="lineCov">        130 :   va_end(argp);</span>
<span class="lineNum">      92 </span><span class="lineCov">        130 :   lua_concat(L, 2);</span>
<span class="lineNum">      93 </span><span class="lineCov">        130 :   return lua_error(L);</span>
<span class="lineNum">      94 </span>            : }
<span class="lineNum">      95 </span>            : 
<span class="lineNum">      96 </span>            : /* }====================================================== */
<span class="lineNum">      97 </span>            : 
<span class="lineNum">      98 </span>            : 
<span class="lineNum">      99 </span><span class="lineCov">         15 : LUALIB_API int luaL_checkoption (lua_State *L, int narg, const char *def,</span>
<span class="lineNum">     100 </span>            :                                  const char *const lst[]) {
<span class="lineNum">     101 </span><span class="lineCov">         15 :   const char *name = (def) ? luaL_optstring(L, narg, def) :</span>
<span class="lineNum">     102 </span><span class="lineCov">          3 :                              luaL_checkstring(L, narg);</span>
<span class="lineNum">     103 </span>            :   int i;
<span class="lineNum">     104 </span><span class="lineCov">         42 :   for (i=0; lst[i]; i++)</span>
<span class="lineNum">     105 </span><span class="lineCov">         40 :     if (strcmp(lst[i], name) == 0)</span>
<span class="lineNum">     106 </span><span class="lineCov">         13 :       return i;</span>
<span class="lineNum">     107 </span><span class="lineCov">          2 :   return luaL_argerror(L, narg,</span>
<span class="lineNum">     108 </span>            :                        lua_pushfstring(L, &quot;invalid option &quot; LUA_QS, name));
<span class="lineNum">     109 </span>            : }
<span class="lineNum">     110 </span>            : 
<span class="lineNum">     111 </span>            : 
<span class="lineNum">     112 </span><span class="lineCov">        174 : LUALIB_API int luaL_newmetatable (lua_State *L, const char *tname) {</span>
<span class="lineNum">     113 </span><span class="lineCov">        174 :   lua_getfield(L, LUA_REGISTRYINDEX, tname);  /* get registry.name */</span>
<span class="lineNum">     114 </span><span class="lineCov">        174 :   if (!lua_isnil(L, -1))  /* name already in use? */</span>
<span class="lineNum">     115 </span><span class="lineNoCov">          0 :     return 0;  /* leave previous value on top, but return 0 */</span>
<span class="lineNum">     116 </span><span class="lineCov">        174 :   lua_pop(L, 1);</span>
<span class="lineNum">     117 </span><span class="lineCov">        174 :   lua_newtable(L);  /* create metatable */</span>
<span class="lineNum">     118 </span><span class="lineCov">        174 :   lua_pushvalue(L, -1);</span>
<span class="lineNum">     119 </span><span class="lineCov">        174 :   lua_setfield(L, LUA_REGISTRYINDEX, tname);  /* registry.name = metatable */</span>
<span class="lineNum">     120 </span><span class="lineCov">        174 :   return 1;</span>
<span class="lineNum">     121 </span>            : }
<span class="lineNum">     122 </span>            : 
<span class="lineNum">     123 </span>            : 
<span class="lineNum">     124 </span><span class="lineCov">        675 : LUALIB_API void *luaL_checkudata (lua_State *L, int ud, const char *tname) {</span>
<span class="lineNum">     125 </span><span class="lineCov">        675 :   void *p = lua_touserdata(L, ud);</span>
<span class="lineNum">     126 </span><span class="lineCov">        675 :   if (p != NULL) {  /* value is a userdata? */</span>
<span class="lineNum">     127 </span><span class="lineCov">        675 :     if (lua_getmetatable(L, ud)) {  /* does it have a metatable? */</span>
<span class="lineNum">     128 </span><span class="lineCov">        675 :       lua_getfield(L, LUA_REGISTRYINDEX, tname);  /* get correct metatable */</span>
<span class="lineNum">     129 </span><span class="lineCov">        675 :       if (lua_rawequal(L, -1, -2)) {  /* does it have the correct mt? */</span>
<span class="lineNum">     130 </span><span class="lineCov">        675 :         lua_pop(L, 2);  /* remove both metatables */</span>
<span class="lineNum">     131 </span><span class="lineCov">        675 :         return p;</span>
<span class="lineNum">     132 </span>            :       }
<span class="lineNum">     133 </span>            :     }
<span class="lineNum">     134 </span>            :   }
<span class="lineNum">     135 </span><span class="lineNoCov">          0 :   luaL_typerror(L, ud, tname);  /* else error */</span>
<span class="lineNum">     136 </span><span class="lineNoCov">          0 :   return NULL;  /* to avoid warnings */</span>
<span class="lineNum">     137 </span>            : }
<span class="lineNum">     138 </span>            : 
<span class="lineNum">     139 </span>            : 
<span class="lineNum">     140 </span><span class="lineCov">       6598 : LUALIB_API void luaL_checkstack (lua_State *L, int space, const char *mes) {</span>
<span class="lineNum">     141 </span><span class="lineCov">       6598 :   if (!lua_checkstack(L, space))</span>
<span class="lineNum">     142 </span><span class="lineNoCov">          0 :     luaL_error(L, &quot;stack overflow (%s)&quot;, mes);</span>
<span class="lineNum">     143 </span><span class="lineCov">       6598 : }</span>
<span class="lineNum">     144 </span>            : 
<span class="lineNum">     145 </span>            : 
<span class="lineNum">     146 </span><span class="lineCov">      70371 : LUALIB_API void luaL_checktype (lua_State *L, int narg, int t) {</span>
<span class="lineNum">     147 </span><span class="lineCov">      70371 :   if (lua_type(L, narg) != t)</span>
<span class="lineNum">     148 </span><span class="lineCov">          1 :     tag_error(L, narg, t);</span>
<span class="lineNum">     149 </span><span class="lineCov">      70370 : }</span>
<span class="lineNum">     150 </span>            : 
<span class="lineNum">     151 </span>            : 
<span class="lineNum">     152 </span><span class="lineCov">      10839 : LUALIB_API void luaL_checkany (lua_State *L, int narg) {</span>
<span class="lineNum">     153 </span><span class="lineCov">      10839 :   if (lua_type(L, narg) == LUA_TNONE)</span>
<span class="lineNum">     154 </span><span class="lineCov">          6 :     luaL_argerror(L, narg, &quot;value expected&quot;);</span>
<span class="lineNum">     155 </span><span class="lineCov">      10833 : }</span>
<span class="lineNum">     156 </span>            : 
<span class="lineNum">     157 </span>            : 
<span class="lineNum">     158 </span><span class="lineCov">      20775 : LUALIB_API const char *luaL_checklstring (lua_State *L, int narg, size_t *len) {</span>
<span class="lineNum">     159 </span><span class="lineCov">      20775 :   const char *s = lua_tolstring(L, narg, len);</span>
<span class="lineNum">     160 </span><span class="lineCov">      20775 :   if (!s) tag_error(L, narg, LUA_TSTRING);</span>
<span class="lineNum">     161 </span><span class="lineCov">      20773 :   return s;</span>
<span class="lineNum">     162 </span>            : }
<span class="lineNum">     163 </span>            : 
<span class="lineNum">     164 </span>            : 
<span class="lineNum">     165 </span><span class="lineCov">      12321 : LUALIB_API const char *luaL_optlstring (lua_State *L, int narg,</span>
<span class="lineNum">     166 </span>            :                                         const char *def, size_t *len) {
<span class="lineNum">     167 </span><span class="lineCov">      12321 :   if (lua_isnoneornil(L, narg)) {</span>
<span class="lineNum">     168 </span><span class="lineCov">        283 :     if (len)</span>
<span class="lineNum">     169 </span><span class="lineCov">          1 :       *len = (def ? strlen(def) : 0);</span>
<span class="lineNum">     170 </span><span class="lineCov">        283 :     return def;</span>
<span class="lineNum">     171 </span>            :   }
<span class="lineNum">     172 </span><span class="lineCov">      12038 :   else return luaL_checklstring(L, narg, len);</span>
<span class="lineNum">     173 </span>            : }
<span class="lineNum">     174 </span>            : 
<span class="lineNum">     175 </span>            : 
<span class="lineNum">     176 </span><span class="lineCov">        132 : LUALIB_API lua_Number luaL_checknumber (lua_State *L, int narg) {</span>
<span class="lineNum">     177 </span><span class="lineCov">        132 :   lua_Number d = lua_tonumber(L, narg);</span>
<span class="lineNum">     178 </span><span class="lineCov">        132 :   if (d == 0 &amp;&amp; !lua_isnumber(L, narg))  /* avoid extra test when d is not 0 */</span>
<span class="lineNum">     179 </span><span class="lineCov">          3 :     tag_error(L, narg, LUA_TNUMBER);</span>
<span class="lineNum">     180 </span><span class="lineCov">        129 :   return d;</span>
<span class="lineNum">     181 </span>            : }
<span class="lineNum">     182 </span>            : 
<span class="lineNum">     183 </span>            : 
<span class="lineNum">     184 </span><span class="lineCov">          1 : LUALIB_API lua_Number luaL_optnumber (lua_State *L, int narg, lua_Number def) {</span>
<span class="lineNum">     185 </span><span class="lineCov">          1 :   return luaL_opt(L, luaL_checknumber, narg, def);</span>
<span class="lineNum">     186 </span>            : }
<span class="lineNum">     187 </span>            : 
<span class="lineNum">     188 </span>            : 
<span class="lineNum">     189 </span><span class="lineCov">      59093 : LUALIB_API lua_Integer luaL_checkinteger (lua_State *L, int narg) {</span>
<span class="lineNum">     190 </span><span class="lineCov">      59093 :   lua_Integer d = lua_tointeger(L, narg);</span>
<span class="lineNum">     191 </span><span class="lineCov">      59093 :   if (d == 0 &amp;&amp; !lua_isnumber(L, narg))  /* avoid extra test when d is not 0 */</span>
<span class="lineNum">     192 </span><span class="lineCov">          2 :     tag_error(L, narg, LUA_TNUMBER);</span>
<span class="lineNum">     193 </span><span class="lineCov">      59091 :   return d;</span>
<span class="lineNum">     194 </span>            : }
<span class="lineNum">     195 </span>            : 
<span class="lineNum">     196 </span>            : 
<span class="lineNum">     197 </span><span class="lineCov">      19125 : LUALIB_API lua_Integer luaL_optinteger (lua_State *L, int narg,</span>
<span class="lineNum">     198 </span>            :                                                       lua_Integer def) {
<span class="lineNum">     199 </span><span class="lineCov">      19125 :   return luaL_opt(L, luaL_checkinteger, narg, def);</span>
<span class="lineNum">     200 </span>            : }
<span class="lineNum">     201 </span>            : 
<span class="lineNum">     202 </span>            : 
<span class="lineNum">     203 </span><span class="lineCov">       4165 : LUALIB_API int luaL_getmetafield (lua_State *L, int obj, const char *event) {</span>
<span class="lineNum">     204 </span><span class="lineCov">       4165 :   if (!lua_getmetatable(L, obj))  /* no metatable? */</span>
<span class="lineNum">     205 </span><span class="lineCov">       1807 :     return 0;</span>
<span class="lineNum">     206 </span><span class="lineCov">       2358 :   lua_pushstring(L, event);</span>
<span class="lineNum">     207 </span><span class="lineCov">       2358 :   lua_rawget(L, -2);</span>
<span class="lineNum">     208 </span><span class="lineCov">       2358 :   if (lua_isnil(L, -1)) {</span>
<span class="lineNum">     209 </span><span class="lineCov">       2315 :     lua_pop(L, 2);  /* remove metatable and metafield */</span>
<span class="lineNum">     210 </span><span class="lineCov">       2315 :     return 0;</span>
<span class="lineNum">     211 </span>            :   }
<span class="lineNum">     212 </span>            :   else {
<span class="lineNum">     213 </span><span class="lineCov">         43 :     lua_remove(L, -2);  /* remove only metatable */</span>
<span class="lineNum">     214 </span><span class="lineCov">         43 :     return 1;</span>
<span class="lineNum">     215 </span>            :   }
<span class="lineNum">     216 </span>            : }
<span class="lineNum">     217 </span>            : 
<span class="lineNum">     218 </span>            : 
<span class="lineNum">     219 </span><span class="lineCov">       4073 : LUALIB_API int luaL_callmeta (lua_State *L, int obj, const char *event) {</span>
<span class="lineNum">     220 </span><span class="lineCov">       4073 :   obj = abs_index(L, obj);</span>
<span class="lineNum">     221 </span><span class="lineCov">       4073 :   if (!luaL_getmetafield(L, obj, event))  /* no metafield? */</span>
<span class="lineNum">     222 </span><span class="lineCov">       4032 :     return 0;</span>
<span class="lineNum">     223 </span><span class="lineCov">         41 :   lua_pushvalue(L, obj);</span>
<span class="lineNum">     224 </span><span class="lineCov">         41 :   lua_call(L, 1, 1);</span>
<span class="lineNum">     225 </span><span class="lineCov">         40 :   return 1;</span>
<span class="lineNum">     226 </span>            : }
<span class="lineNum">     227 </span>            : 
<span class="lineNum">     228 </span>            : 
<span class="lineNum">     229 </span><span class="lineCov">        957 : LUALIB_API void (luaL_register) (lua_State *L, const char *libname,</span>
<span class="lineNum">     230 </span>            :                                 const luaL_Reg *l) {
<span class="lineNum">     231 </span><span class="lineCov">        957 :   luaI_openlib(L, libname, l, 0);</span>
<span class="lineNum">     232 </span><span class="lineCov">        957 : }</span>
<span class="lineNum">     233 </span>            : 
<span class="lineNum">     234 </span>            : 
<span class="lineNum">     235 </span><span class="lineCov">        783 : static int libsize (const luaL_Reg *l) {</span>
<span class="lineNum">     236 </span><span class="lineCov">        783 :   int size = 0;</span>
<span class="lineNum">     237 </span><span class="lineCov">      11223 :   for (; l-&gt;name; l++) size++;</span>
<span class="lineNum">     238 </span><span class="lineCov">        783 :   return size;</span>
<span class="lineNum">     239 </span>            : }
<span class="lineNum">     240 </span>            : 
<span class="lineNum">     241 </span>            : 
<span class="lineNum">     242 </span><span class="lineCov">        957 : LUALIB_API void luaI_openlib (lua_State *L, const char *libname,</span>
<span class="lineNum">     243 </span>            :                               const luaL_Reg *l, int nup) {
<span class="lineNum">     244 </span><span class="lineCov">        957 :   if (libname) {</span>
<span class="lineNum">     245 </span><span class="lineCov">        783 :     int size = libsize(l);</span>
<span class="lineNum">     246 </span>            :     /* check whether lib already exists */
<span class="lineNum">     247 </span><span class="lineCov">        783 :     luaL_findtable(L, LUA_REGISTRYINDEX, &quot;_LOADED&quot;, 1);</span>
<span class="lineNum">     248 </span><span class="lineCov">        783 :     lua_getfield(L, -1, libname);  /* get _LOADED[libname] */</span>
<span class="lineNum">     249 </span><span class="lineCov">        783 :     if (!lua_istable(L, -1)) {  /* not found? */</span>
<span class="lineNum">     250 </span><span class="lineCov">        783 :       lua_pop(L, 1);  /* remove previous result */</span>
<span class="lineNum">     251 </span>            :       /* try global variable (and create one if it does not exist) */
<span class="lineNum">     252 </span><span class="lineCov">        783 :       if (luaL_findtable(L, LUA_GLOBALSINDEX, libname, size) != NULL)</span>
<span class="lineNum">     253 </span><span class="lineNoCov">          0 :         luaL_error(L, &quot;name conflict for module &quot; LUA_QS, libname);</span>
<span class="lineNum">     254 </span><span class="lineCov">        783 :       lua_pushvalue(L, -1);</span>
<span class="lineNum">     255 </span><span class="lineCov">        783 :       lua_setfield(L, -3, libname);  /* _LOADED[libname] = new table */</span>
<span class="lineNum">     256 </span>            :     }
<span class="lineNum">     257 </span><span class="lineCov">        783 :     lua_remove(L, -2);  /* remove _LOADED table */</span>
<span class="lineNum">     258 </span><span class="lineCov">        783 :     lua_insert(L, -(nup+1));  /* move library table to below upvalues */</span>
<span class="lineNum">     259 </span>            :   }
<span class="lineNum">     260 </span><span class="lineCov">      12354 :   for (; l-&gt;name; l++) {</span>
<span class="lineNum">     261 </span>            :     int i;
<span class="lineNum">     262 </span><span class="lineCov">      11397 :     for (i=0; i&lt;nup; i++)  /* copy upvalues to the top */</span>
<span class="lineNum">     263 </span><span class="lineNoCov">          0 :       lua_pushvalue(L, -nup);</span>
<span class="lineNum">     264 </span><span class="lineCov">      11397 :     lua_pushcclosure(L, l-&gt;func, nup);</span>
<span class="lineNum">     265 </span><span class="lineCov">      11397 :     lua_setfield(L, -(nup+2), l-&gt;name);</span>
<span class="lineNum">     266 </span>            :   }
<span class="lineNum">     267 </span><span class="lineCov">        957 :   lua_pop(L, nup);  /* remove upvalues */</span>
<span class="lineNum">     268 </span><span class="lineCov">        957 : }</span>
<span class="lineNum">     269 </span>            : 
<span class="lineNum">     270 </span>            : 
<span class="lineNum">     271 </span>            : 
<span class="lineNum">     272 </span>            : /*
<span class="lineNum">     273 </span>            : ** {======================================================
<span class="lineNum">     274 </span>            : ** getn-setn: size for arrays
<span class="lineNum">     275 </span>            : ** =======================================================
<span class="lineNum">     276 </span>            : */
<span class="lineNum">     277 </span>            : 
<span class="lineNum">     278 </span>            : #if defined(LUA_COMPAT_GETN)
<span class="lineNum">     279 </span>            : 
<span class="lineNum">     280 </span>            : static int checkint (lua_State *L, int topop) {
<span class="lineNum">     281 </span>            :   int n = (lua_type(L, -1) == LUA_TNUMBER) ? lua_tointeger(L, -1) : -1;
<span class="lineNum">     282 </span>            :   lua_pop(L, topop);
<span class="lineNum">     283 </span>            :   return n;
<span class="lineNum">     284 </span>            : }
<span class="lineNum">     285 </span>            : 
<span class="lineNum">     286 </span>            : 
<span class="lineNum">     287 </span>            : static void getsizes (lua_State *L) {
<span class="lineNum">     288 </span>            :   lua_getfield(L, LUA_REGISTRYINDEX, &quot;LUA_SIZES&quot;);
<span class="lineNum">     289 </span>            :   if (lua_isnil(L, -1)) {  /* no `size' table? */
<span class="lineNum">     290 </span>            :     lua_pop(L, 1);  /* remove nil */
<span class="lineNum">     291 </span>            :     lua_newtable(L);  /* create it */
<span class="lineNum">     292 </span>            :     lua_pushvalue(L, -1);  /* `size' will be its own metatable */
<span class="lineNum">     293 </span>            :     lua_setmetatable(L, -2);
<span class="lineNum">     294 </span>            :     lua_pushliteral(L, &quot;kv&quot;);
<span class="lineNum">     295 </span>            :     lua_setfield(L, -2, &quot;__mode&quot;);  /* metatable(N).__mode = &quot;kv&quot; */
<span class="lineNum">     296 </span>            :     lua_pushvalue(L, -1);
<span class="lineNum">     297 </span>            :     lua_setfield(L, LUA_REGISTRYINDEX, &quot;LUA_SIZES&quot;);  /* store in register */
<span class="lineNum">     298 </span>            :   }
<span class="lineNum">     299 </span>            : }
<span class="lineNum">     300 </span>            : 
<span class="lineNum">     301 </span>            : 
<span class="lineNum">     302 </span>            : LUALIB_API void luaL_setn (lua_State *L, int t, int n) {
<span class="lineNum">     303 </span>            :   t = abs_index(L, t);
<span class="lineNum">     304 </span>            :   lua_pushliteral(L, &quot;n&quot;);
<span class="lineNum">     305 </span>            :   lua_rawget(L, t);
<span class="lineNum">     306 </span>            :   if (checkint(L, 1) &gt;= 0) {  /* is there a numeric field `n'? */
<span class="lineNum">     307 </span>            :     lua_pushliteral(L, &quot;n&quot;);  /* use it */
<span class="lineNum">     308 </span>            :     lua_pushinteger(L, n);
<span class="lineNum">     309 </span>            :     lua_rawset(L, t);
<span class="lineNum">     310 </span>            :   }
<span class="lineNum">     311 </span>            :   else {  /* use `sizes' */
<span class="lineNum">     312 </span>            :     getsizes(L);
<span class="lineNum">     313 </span>            :     lua_pushvalue(L, t);
<span class="lineNum">     314 </span>            :     lua_pushinteger(L, n);
<span class="lineNum">     315 </span>            :     lua_rawset(L, -3);  /* sizes[t] = n */
<span class="lineNum">     316 </span>            :     lua_pop(L, 1);  /* remove `sizes' */
<span class="lineNum">     317 </span>            :   }
<span class="lineNum">     318 </span>            : }
<span class="lineNum">     319 </span>            : 
<span class="lineNum">     320 </span>            : 
<span class="lineNum">     321 </span>            : LUALIB_API int luaL_getn (lua_State *L, int t) {
<span class="lineNum">     322 </span>            :   int n;
<span class="lineNum">     323 </span>            :   t = abs_index(L, t);
<span class="lineNum">     324 </span>            :   lua_pushliteral(L, &quot;n&quot;);  /* try t.n */
<span class="lineNum">     325 </span>            :   lua_rawget(L, t);
<span class="lineNum">     326 </span>            :   if ((n = checkint(L, 1)) &gt;= 0) return n;
<span class="lineNum">     327 </span>            :   getsizes(L);  /* else try sizes[t] */
<span class="lineNum">     328 </span>            :   lua_pushvalue(L, t);
<span class="lineNum">     329 </span>            :   lua_rawget(L, -2);
<span class="lineNum">     330 </span>            :   if ((n = checkint(L, 2)) &gt;= 0) return n;
<span class="lineNum">     331 </span>            :   return (int)lua_objlen(L, t);
<span class="lineNum">     332 </span>            : }
<span class="lineNum">     333 </span>            : 
<span class="lineNum">     334 </span>            : #endif
<span class="lineNum">     335 </span>            : 
<span class="lineNum">     336 </span>            : /* }====================================================== */
<span class="lineNum">     337 </span>            : 
<span class="lineNum">     338 </span>            : 
<span class="lineNum">     339 </span>            : 
<span class="lineNum">     340 </span><span class="lineCov">       1121 : LUALIB_API const char *luaL_gsub (lua_State *L, const char *s, const char *p,</span>
<span class="lineNum">     341 </span>            :                                                                const char *r) {
<span class="lineNum">     342 </span>            :   const char *wild;
<span class="lineNum">     343 </span><span class="lineCov">       1121 :   size_t l = strlen(p);</span>
<span class="lineNum">     344 </span>            :   luaL_Buffer b;
<span class="lineNum">     345 </span><span class="lineCov">       1121 :   luaL_buffinit(L, &amp;b);</span>
<span class="lineNum">     346 </span><span class="lineCov">       2001 :   while ((wild = strstr(s, p)) != NULL) {</span>
<span class="lineNum">     347 </span><span class="lineCov">        880 :     luaL_addlstring(&amp;b, s, wild - s);  /* push prefix */</span>
<span class="lineNum">     348 </span><span class="lineCov">        880 :     luaL_addstring(&amp;b, r);  /* push replacement in place of pattern */</span>
<span class="lineNum">     349 </span><span class="lineCov">        880 :     s = wild + l;  /* continue after `p' */</span>
<span class="lineNum">     350 </span>            :   }
<span class="lineNum">     351 </span><span class="lineCov">       1121 :   luaL_addstring(&amp;b, s);  /* push last suffix */</span>
<span class="lineNum">     352 </span><span class="lineCov">       1121 :   luaL_pushresult(&amp;b);</span>
<span class="lineNum">     353 </span><span class="lineCov">       1121 :   return lua_tostring(L, -1);</span>
<span class="lineNum">     354 </span>            : }
<span class="lineNum">     355 </span>            : 
<span class="lineNum">     356 </span>            : 
<span class="lineNum">     357 </span><span class="lineCov">       1655 : LUALIB_API const char *luaL_findtable (lua_State *L, int idx,</span>
<span class="lineNum">     358 </span>            :                                        const char *fname, int szhint) {
<span class="lineNum">     359 </span>            :   const char *e;
<span class="lineNum">     360 </span><span class="lineCov">       1655 :   lua_pushvalue(L, idx);</span>
<span class="lineNum">     361 </span>            :   do {
<span class="lineNum">     362 </span><span class="lineCov">       1655 :     e = strchr(fname, '.');</span>
<span class="lineNum">     363 </span><span class="lineCov">       1655 :     if (e == NULL) e = fname + strlen(fname);</span>
<span class="lineNum">     364 </span><span class="lineCov">       1655 :     lua_pushlstring(L, fname, e - fname);</span>
<span class="lineNum">     365 </span><span class="lineCov">       1655 :     lua_rawget(L, -2);</span>
<span class="lineNum">     366 </span><span class="lineCov">       1655 :     if (lua_isnil(L, -1)) {  /* no such field? */</span>
<span class="lineNum">     367 </span><span class="lineCov">        785 :       lua_pop(L, 1);  /* remove this nil */</span>
<span class="lineNum">     368 </span><span class="lineCov">        785 :       lua_createtable(L, 0, (*e == '.' ? 1 : szhint)); /* new table for field */</span>
<span class="lineNum">     369 </span><span class="lineCov">        785 :       lua_pushlstring(L, fname, e - fname);</span>
<span class="lineNum">     370 </span><span class="lineCov">        785 :       lua_pushvalue(L, -2);</span>
<span class="lineNum">     371 </span><span class="lineCov">        785 :       lua_settable(L, -4);  /* set new table into field */</span>
<span class="lineNum">     372 </span>            :     }
<span class="lineNum">     373 </span><span class="lineCov">        870 :     else if (!lua_istable(L, -1)) {  /* field has a non-table value? */</span>
<span class="lineNum">     374 </span><span class="lineNoCov">          0 :       lua_pop(L, 2);  /* remove table and value */</span>
<span class="lineNum">     375 </span><span class="lineNoCov">          0 :       return fname;  /* return problematic part of the name */</span>
<span class="lineNum">     376 </span>            :     }
<span class="lineNum">     377 </span><span class="lineCov">       1655 :     lua_remove(L, -2);  /* remove previous table */</span>
<span class="lineNum">     378 </span><span class="lineCov">       1655 :     fname = e + 1;</span>
<span class="lineNum">     379 </span><span class="lineCov">       1655 :   } while (*e == '.');</span>
<span class="lineNum">     380 </span><span class="lineCov">       1655 :   return NULL;</span>
<span class="lineNum">     381 </span>            : }
<span class="lineNum">     382 </span>            : 
<span class="lineNum">     383 </span>            : 
<span class="lineNum">     384 </span>            : 
<span class="lineNum">     385 </span>            : /*
<span class="lineNum">     386 </span>            : ** {======================================================
<span class="lineNum">     387 </span>            : ** Generic Buffer manipulation
<span class="lineNum">     388 </span>            : ** =======================================================
<span class="lineNum">     389 </span>            : */
<span class="lineNum">     390 </span>            : 
<span class="lineNum">     391 </span>            : 
<span class="lineNum">     392 </span>            : #define bufflen(B)      ((B)-&gt;p - (B)-&gt;buffer)
<span class="lineNum">     393 </span>            : #define bufffree(B)     ((size_t)(LUAL_BUFFERSIZE - bufflen(B)))
<span class="lineNum">     394 </span>            : 
<span class="lineNum">     395 </span>            : #define LIMIT   (LUA_MINSTACK/2)
<span class="lineNum">     396 </span>            : 
<span class="lineNum">     397 </span>            : 
<span class="lineNum">     398 </span><span class="lineCov">      13687 : static int emptybuffer (luaL_Buffer *B) {</span>
<span class="lineNum">     399 </span><span class="lineCov">      13687 :   size_t l = bufflen(B);</span>
<span class="lineNum">     400 </span><span class="lineCov">      13687 :   if (l == 0) return 0;  /* put nothing on stack */</span>
<span class="lineNum">     401 </span>            :   else {
<span class="lineNum">     402 </span><span class="lineCov">      13398 :     lua_pushlstring(B-&gt;L, B-&gt;buffer, l);</span>
<span class="lineNum">     403 </span><span class="lineCov">      13398 :     B-&gt;p = B-&gt;buffer;</span>
<span class="lineNum">     404 </span><span class="lineCov">      13398 :     B-&gt;lvl++;</span>
<span class="lineNum">     405 </span><span class="lineCov">      13398 :     return 1;</span>
<span class="lineNum">     406 </span>            :   }
<span class="lineNum">     407 </span>            : }
<span class="lineNum">     408 </span>            : 
<span class="lineNum">     409 </span>            : 
<span class="lineNum">     410 </span><span class="lineCov">         12 : static void adjuststack (luaL_Buffer *B) {</span>
<span class="lineNum">     411 </span><span class="lineCov">         12 :   if (B-&gt;lvl &gt; 1) {</span>
<span class="lineNum">     412 </span><span class="lineCov">          9 :     lua_State *L = B-&gt;L;</span>
<span class="lineNum">     413 </span><span class="lineCov">          9 :     int toget = 1;  /* number of levels to concat */</span>
<span class="lineNum">     414 </span><span class="lineCov">          9 :     size_t toplen = lua_strlen(L, -1);</span>
<span class="lineNum">     415 </span>            :     do {
<span class="lineNum">     416 </span><span class="lineCov">          9 :       size_t l = lua_strlen(L, -(toget+1));</span>
<span class="lineNum">     417 </span><span class="lineCov">          9 :       if (B-&gt;lvl - toget + 1 &gt;= LIMIT || toplen &gt; l) {</span>
<span class="lineNum">     418 </span><span class="lineNoCov">          0 :         toplen += l;</span>
<span class="lineNum">     419 </span><span class="lineNoCov">          0 :         toget++;</span>
<span class="lineNum">     420 </span>            :       }
<span class="lineNum">     421 </span>            :       else break;
<span class="lineNum">     422 </span><span class="lineNoCov">          0 :     } while (toget &lt; B-&gt;lvl);</span>
<span class="lineNum">     423 </span><span class="lineCov">          9 :     lua_concat(L, toget);</span>
<span class="lineNum">     424 </span><span class="lineCov">          9 :     B-&gt;lvl = B-&gt;lvl - toget + 1;</span>
<span class="lineNum">     425 </span>            :   }
<span class="lineNum">     426 </span><span class="lineCov">         12 : }</span>
<span class="lineNum">     427 </span>            : 
<span class="lineNum">     428 </span>            : 
<span class="lineNum">     429 </span><span class="lineCov">        268 : LUALIB_API char *luaL_prepbuffer (luaL_Buffer *B) {</span>
<span class="lineNum">     430 </span><span class="lineCov">        268 :   if (emptybuffer(B))</span>
<span class="lineNum">     431 </span><span class="lineCov">         12 :     adjuststack(B);</span>
<span class="lineNum">     432 </span><span class="lineCov">        268 :   return B-&gt;buffer;</span>
<span class="lineNum">     433 </span>            : }
<span class="lineNum">     434 </span>            : 
<span class="lineNum">     435 </span>            : 
<span class="lineNum">     436 </span><span class="lineCov">      92107 : LUALIB_API void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l) {</span>
<span class="lineNum">     437 </span><span class="lineCov">     277534 :   while (l--)</span>
<span class="lineNum">     438 </span><span class="lineCov">     185427 :     luaL_addchar(B, *s++);</span>
<span class="lineNum">     439 </span><span class="lineCov">      92107 : }</span>
<span class="lineNum">     440 </span>            : 
<span class="lineNum">     441 </span>            : 
<span class="lineNum">     442 </span><span class="lineCov">       2001 : LUALIB_API void luaL_addstring (luaL_Buffer *B, const char *s) {</span>
<span class="lineNum">     443 </span><span class="lineCov">       2001 :   luaL_addlstring(B, s, strlen(s));</span>
<span class="lineNum">     444 </span><span class="lineCov">       2001 : }</span>
<span class="lineNum">     445 </span>            : 
<span class="lineNum">     446 </span>            : 
<span class="lineNum">     447 </span><span class="lineCov">      13419 : LUALIB_API void luaL_pushresult (luaL_Buffer *B) {</span>
<span class="lineNum">     448 </span><span class="lineCov">      13419 :   emptybuffer(B);</span>
<span class="lineNum">     449 </span><span class="lineCov">      13419 :   lua_concat(B-&gt;L, B-&gt;lvl);</span>
<span class="lineNum">     450 </span><span class="lineCov">      13419 :   B-&gt;lvl = 1;</span>
<span class="lineNum">     451 </span><span class="lineCov">      13419 : }</span>
<span class="lineNum">     452 </span>            : 
<span class="lineNum">     453 </span>            : 
<span class="lineNum">     454 </span><span class="lineCov">      80923 : LUALIB_API void luaL_addvalue (luaL_Buffer *B) {</span>
<span class="lineNum">     455 </span><span class="lineCov">      80923 :   lua_State *L = B-&gt;L;</span>
<span class="lineNum">     456 </span>            :   size_t vl;
<span class="lineNum">     457 </span><span class="lineCov">      80923 :   const char *s = lua_tolstring(L, -1, &amp;vl);</span>
<span class="lineNum">     458 </span><span class="lineCov">      80923 :   if (vl &lt;= bufffree(B)) {  /* fit into buffer? */</span>
<span class="lineNum">     459 </span><span class="lineCov">      80923 :     memcpy(B-&gt;p, s, vl);  /* put it there */</span>
<span class="lineNum">     460 </span><span class="lineCov">      80923 :     B-&gt;p += vl;</span>
<span class="lineNum">     461 </span><span class="lineCov">      80923 :     lua_pop(L, 1);  /* remove from stack */</span>
<span class="lineNum">     462 </span>            :   }
<span class="lineNum">     463 </span>            :   else {
<span class="lineNum">     464 </span><span class="lineNoCov">          0 :     if (emptybuffer(B))</span>
<span class="lineNum">     465 </span><span class="lineNoCov">          0 :       lua_insert(L, -2);  /* put buffer before new value */</span>
<span class="lineNum">     466 </span><span class="lineNoCov">          0 :     B-&gt;lvl++;  /* add new value into B stack */</span>
<span class="lineNum">     467 </span><span class="lineNoCov">          0 :     adjuststack(B);</span>
<span class="lineNum">     468 </span>            :   }
<span class="lineNum">     469 </span><span class="lineCov">      80923 : }</span>
<span class="lineNum">     470 </span>            : 
<span class="lineNum">     471 </span>            : 
<span class="lineNum">     472 </span><span class="lineCov">      13436 : LUALIB_API void luaL_buffinit (lua_State *L, luaL_Buffer *B) {</span>
<span class="lineNum">     473 </span><span class="lineCov">      13436 :   B-&gt;L = L;</span>
<span class="lineNum">     474 </span><span class="lineCov">      13436 :   B-&gt;p = B-&gt;buffer;</span>
<span class="lineNum">     475 </span><span class="lineCov">      13436 :   B-&gt;lvl = 0;</span>
<span class="lineNum">     476 </span><span class="lineCov">      13436 : }</span>
<span class="lineNum">     477 </span>            : 
<span class="lineNum">     478 </span>            : /* }====================================================== */
<span class="lineNum">     479 </span>            : 
<span class="lineNum">     480 </span>            : 
<span class="lineNum">     481 </span><span class="lineNoCov">          0 : LUALIB_API int luaL_ref (lua_State *L, int t) {</span>
<span class="lineNum">     482 </span>            :   int ref;
<span class="lineNum">     483 </span><span class="lineNoCov">          0 :   t = abs_index(L, t);</span>
<span class="lineNum">     484 </span><span class="lineNoCov">          0 :   if (lua_isnil(L, -1)) {</span>
<span class="lineNum">     485 </span><span class="lineNoCov">          0 :     lua_pop(L, 1);  /* remove from stack */</span>
<span class="lineNum">     486 </span><span class="lineNoCov">          0 :     return LUA_REFNIL;  /* `nil' has a unique fixed reference */</span>
<span class="lineNum">     487 </span>            :   }
<span class="lineNum">     488 </span><span class="lineNoCov">          0 :   lua_rawgeti(L, t, FREELIST_REF);  /* get first free element */</span>
<span class="lineNum">     489 </span><span class="lineNoCov">          0 :   ref = (int)lua_tointeger(L, -1);  /* ref = t[FREELIST_REF] */</span>
<span class="lineNum">     490 </span><span class="lineNoCov">          0 :   lua_pop(L, 1);  /* remove it from stack */</span>
<span class="lineNum">     491 </span><span class="lineNoCov">          0 :   if (ref != 0) {  /* any free element? */</span>
<span class="lineNum">     492 </span><span class="lineNoCov">          0 :     lua_rawgeti(L, t, ref);  /* remove it from list */</span>
<span class="lineNum">     493 </span><span class="lineNoCov">          0 :     lua_rawseti(L, t, FREELIST_REF);  /* (t[FREELIST_REF] = t[ref]) */</span>
<span class="lineNum">     494 </span>            :   }
<span class="lineNum">     495 </span>            :   else {  /* no free elements */
<span class="lineNum">     496 </span><span class="lineNoCov">          0 :     ref = (int)lua_objlen(L, t);</span>
<span class="lineNum">     497 </span><span class="lineNoCov">          0 :     ref++;  /* create new reference */</span>
<span class="lineNum">     498 </span>            :   }
<span class="lineNum">     499 </span><span class="lineNoCov">          0 :   lua_rawseti(L, t, ref);</span>
<span class="lineNum">     500 </span><span class="lineNoCov">          0 :   return ref;</span>
<span class="lineNum">     501 </span>            : }
<span class="lineNum">     502 </span>            : 
<span class="lineNum">     503 </span>            : 
<span class="lineNum">     504 </span><span class="lineNoCov">          0 : LUALIB_API void luaL_unref (lua_State *L, int t, int ref) {</span>
<span class="lineNum">     505 </span><span class="lineNoCov">          0 :   if (ref &gt;= 0) {</span>
<span class="lineNum">     506 </span><span class="lineNoCov">          0 :     t = abs_index(L, t);</span>
<span class="lineNum">     507 </span><span class="lineNoCov">          0 :     lua_rawgeti(L, t, FREELIST_REF);</span>
<span class="lineNum">     508 </span><span class="lineNoCov">          0 :     lua_rawseti(L, t, ref);  /* t[ref] = t[FREELIST_REF] */</span>
<span class="lineNum">     509 </span><span class="lineNoCov">          0 :     lua_pushinteger(L, ref);</span>
<span class="lineNum">     510 </span><span class="lineNoCov">          0 :     lua_rawseti(L, t, FREELIST_REF);  /* t[FREELIST_REF] = ref */</span>
<span class="lineNum">     511 </span>            :   }
<span class="lineNum">     512 </span><span class="lineNoCov">          0 : }</span>
<span class="lineNum">     513 </span>            : 
<span class="lineNum">     514 </span>            : 
<span class="lineNum">     515 </span>            : 
<span class="lineNum">     516 </span>            : /*
<span class="lineNum">     517 </span>            : ** {======================================================
<span class="lineNum">     518 </span>            : ** Load functions
<span class="lineNum">     519 </span>            : ** =======================================================
<span class="lineNum">     520 </span>            : */
<span class="lineNum">     521 </span>            : 
<span class="lineNum">     522 </span>            : typedef struct LoadF {
<span class="lineNum">     523 </span>            :   int extraline;
<span class="lineNum">     524 </span>            :   FILE *f;
<span class="lineNum">     525 </span>            :   char buff[LUAL_BUFFERSIZE];
<span class="lineNum">     526 </span>            : } LoadF;
<span class="lineNum">     527 </span>            : 
<span class="lineNum">     528 </span>            : 
<span class="lineNum">     529 </span><span class="lineCov">        393 : static const char *getF (lua_State *L, void *ud, size_t *size) {</span>
<span class="lineNum">     530 </span><span class="lineCov">        393 :   LoadF *lf = (LoadF *)ud;</span>
<span class="lineNum">     531 </span>            :   (void)L;
<span class="lineNum">     532 </span><span class="lineCov">        393 :   if (lf-&gt;extraline) {</span>
<span class="lineNum">     533 </span><span class="lineCov">         49 :     lf-&gt;extraline = 0;</span>
<span class="lineNum">     534 </span><span class="lineCov">         49 :     *size = 1;</span>
<span class="lineNum">     535 </span><span class="lineCov">         49 :     return &quot;\n&quot;;</span>
<span class="lineNum">     536 </span>            :   }
<span class="lineNum">     537 </span><span class="lineCov">        344 :   if (feof(lf-&gt;f)) return NULL;</span>
<span class="lineNum">     538 </span><span class="lineCov">        182 :   *size = fread(lf-&gt;buff, 1, sizeof(lf-&gt;buff), lf-&gt;f);</span>
<span class="lineNum">     539 </span><span class="lineCov">        182 :   return (*size &gt; 0) ? lf-&gt;buff : NULL;</span>
<span class="lineNum">     540 </span>            : }
<span class="lineNum">     541 </span>            : 
<span class="lineNum">     542 </span>            : 
<span class="lineNum">     543 </span><span class="lineCov">          4 : static int errfile (lua_State *L, const char *what, int fnameindex) {</span>
<span class="lineNum">     544 </span><span class="lineCov">          4 :   const char *serr = strerror(errno);</span>
<span class="lineNum">     545 </span><span class="lineCov">          4 :   const char *filename = lua_tostring(L, fnameindex) + 1;</span>
<span class="lineNum">     546 </span><span class="lineCov">          4 :   lua_pushfstring(L, &quot;cannot %s %s: %s&quot;, what, filename, serr);</span>
<span class="lineNum">     547 </span><span class="lineCov">          4 :   lua_remove(L, fnameindex);</span>
<span class="lineNum">     548 </span><span class="lineCov">          4 :   return LUA_ERRFILE;</span>
<span class="lineNum">     549 </span>            : }
<span class="lineNum">     550 </span>            : 
<span class="lineNum">     551 </span>            : 
<span class="lineNum">     552 </span><span class="lineCov">        176 : LUALIB_API int luaL_loadfile (lua_State *L, const char *filename) {</span>
<span class="lineNum">     553 </span>            :   LoadF lf;
<span class="lineNum">     554 </span>            :   int status, readstatus;
<span class="lineNum">     555 </span>            :   int c;
<span class="lineNum">     556 </span><span class="lineCov">        176 :   int fnameindex = lua_gettop(L) + 1;  /* index of filename on the stack */</span>
<span class="lineNum">     557 </span><span class="lineCov">        176 :   lf.extraline = 0;</span>
<span class="lineNum">     558 </span><span class="lineCov">        176 :   if (filename == NULL) {</span>
<span class="lineNum">     559 </span><span class="lineCov">          6 :     lua_pushliteral(L, &quot;=stdin&quot;);</span>
<span class="lineNum">     560 </span><span class="lineCov">          6 :     lf.f = stdin;</span>
<span class="lineNum">     561 </span>            :   }
<span class="lineNum">     562 </span>            :   else {
<span class="lineNum">     563 </span><span class="lineCov">        170 :     lua_pushfstring(L, &quot;@%s&quot;, filename);</span>
<span class="lineNum">     564 </span><span class="lineCov">        170 :     lf.f = fopen(filename, &quot;r&quot;);</span>
<span class="lineNum">     565 </span><span class="lineCov">        170 :     if (lf.f == NULL) return errfile(L, &quot;open&quot;, fnameindex);</span>
<span class="lineNum">     566 </span>            :   }
<span class="lineNum">     567 </span><span class="lineCov">        172 :   c = getc(lf.f);</span>
<span class="lineNum">     568 </span><span class="lineCov">        172 :   if (c == '#') {  /* Unix exec. file? */</span>
<span class="lineNum">     569 </span><span class="lineCov">         49 :     lf.extraline = 1;</span>
<span class="lineNum">     570 </span><span class="lineCov">        735 :     while ((c = getc(lf.f)) != EOF &amp;&amp; c != '\n') ;  /* skip first line */</span>
<span class="lineNum">     571 </span><span class="lineCov">         49 :     if (c == '\n') c = getc(lf.f);</span>
<span class="lineNum">     572 </span>            :   }
<span class="lineNum">     573 </span><span class="lineCov">        172 :   if (c == LUA_SIGNATURE[0] &amp;&amp; filename) {  /* binary file? */</span>
<span class="lineNum">     574 </span><span class="lineCov">          6 :     lf.f = freopen(filename, &quot;rb&quot;, lf.f);  /* reopen in binary mode */</span>
<span class="lineNum">     575 </span><span class="lineCov">          6 :     if (lf.f == NULL) return errfile(L, &quot;reopen&quot;, fnameindex);</span>
<span class="lineNum">     576 </span>            :     /* skip eventual `#!...' */
<span class="lineNum">     577 </span><span class="lineCov">          6 :    while ((c = getc(lf.f)) != EOF &amp;&amp; c != LUA_SIGNATURE[0]) ;</span>
<span class="lineNum">     578 </span><span class="lineCov">          6 :     lf.extraline = 0;</span>
<span class="lineNum">     579 </span>            :   }
<span class="lineNum">     580 </span><span class="lineCov">        172 :   ungetc(c, lf.f);</span>
<span class="lineNum">     581 </span><span class="lineCov">        172 :   status = lua_load(L, getF, &amp;lf, lua_tostring(L, -1));</span>
<span class="lineNum">     582 </span><span class="lineCov">        172 :   readstatus = ferror(lf.f);</span>
<span class="lineNum">     583 </span><span class="lineCov">        172 :   if (filename) fclose(lf.f);  /* close file (even in case of errors) */</span>
<span class="lineNum">     584 </span><span class="lineCov">        172 :   if (readstatus) {</span>
<span class="lineNum">     585 </span><span class="lineNoCov">          0 :     lua_settop(L, fnameindex);  /* ignore results from `lua_load' */</span>
<span class="lineNum">     586 </span><span class="lineNoCov">          0 :     return errfile(L, &quot;read&quot;, fnameindex);</span>
<span class="lineNum">     587 </span>            :   }
<span class="lineNum">     588 </span><span class="lineCov">        172 :   lua_remove(L, fnameindex);</span>
<span class="lineNum">     589 </span><span class="lineCov">        172 :   return status;</span>
<span class="lineNum">     590 </span>            : }
<span class="lineNum">     591 </span>            : 
<span class="lineNum">     592 </span>            : 
<span class="lineNum">     593 </span>            : typedef struct LoadS {
<span class="lineNum">     594 </span>            :   const char *s;
<span class="lineNum">     595 </span>            :   size_t size;
<span class="lineNum">     596 </span>            : } LoadS;
<span class="lineNum">     597 </span>            : 
<span class="lineNum">     598 </span>            : 
<span class="lineNum">     599 </span><span class="lineCov">        605 : static const char *getS (lua_State *L, void *ud, size_t *size) {</span>
<span class="lineNum">     600 </span><span class="lineCov">        605 :   LoadS *ls = (LoadS *)ud;</span>
<span class="lineNum">     601 </span>            :   (void)L;
<span class="lineNum">     602 </span><span class="lineCov">        605 :   if (ls-&gt;size == 0) return NULL;</span>
<span class="lineNum">     603 </span><span class="lineCov">        311 :   *size = ls-&gt;size;</span>
<span class="lineNum">     604 </span><span class="lineCov">        311 :   ls-&gt;size = 0;</span>
<span class="lineNum">     605 </span><span class="lineCov">        311 :   return ls-&gt;s;</span>
<span class="lineNum">     606 </span>            : }
<span class="lineNum">     607 </span>            : 
<span class="lineNum">     608 </span>            : 
<span class="lineNum">     609 </span><span class="lineCov">        311 : LUALIB_API int luaL_loadbuffer (lua_State *L, const char *buff, size_t size,</span>
<span class="lineNum">     610 </span>            :                                 const char *name) {
<span class="lineNum">     611 </span>            :   LoadS ls;
<span class="lineNum">     612 </span><span class="lineCov">        311 :   ls.s = buff;</span>
<span class="lineNum">     613 </span><span class="lineCov">        311 :   ls.size = size;</span>
<span class="lineNum">     614 </span><span class="lineCov">        311 :   return lua_load(L, getS, &amp;ls, name);</span>
<span class="lineNum">     615 </span>            : }
<span class="lineNum">     616 </span>            : 
<span class="lineNum">     617 </span>            : 
<span class="lineNum">     618 </span><span class="lineNoCov">          0 : LUALIB_API int (luaL_loadstring) (lua_State *L, const char *s) {</span>
<span class="lineNum">     619 </span><span class="lineNoCov">          0 :   return luaL_loadbuffer(L, s, strlen(s), s);</span>
<span class="lineNum">     620 </span>            : }
<span class="lineNum">     621 </span>            : 
<span class="lineNum">     622 </span>            : 
<span class="lineNum">     623 </span>            : 
<span class="lineNum">     624 </span>            : /* }====================================================== */
<span class="lineNum">     625 </span>            : 
<span class="lineNum">     626 </span>            : 
<span class="lineNum">     627 </span><span class="lineCov">     258496 : static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) {</span>
<span class="lineNum">     628 </span>            :   (void)ud;
<span class="lineNum">     629 </span>            :   (void)osize;
<span class="lineNum">     630 </span><span class="lineCov">     258496 :   if (nsize == 0) {</span>
<span class="lineNum">     631 </span><span class="lineCov">     119271 :     free(ptr);</span>
<span class="lineNum">     632 </span><span class="lineCov">     119271 :     return NULL;</span>
<span class="lineNum">     633 </span>            :   }
<span class="lineNum">     634 </span>            :   else
<span class="lineNum">     635 </span><span class="lineCov">     139225 :     return realloc(ptr, nsize);</span>
<span class="lineNum">     636 </span>            : }
<span class="lineNum">     637 </span>            : 
<span class="lineNum">     638 </span>            : 
<span class="lineNum">     639 </span><span class="lineNoCov">          0 : static int panic (lua_State *L) {</span>
<span class="lineNum">     640 </span>            :   (void)L;  /* to avoid warnings */
<span class="lineNum">     641 </span><span class="lineNoCov">          0 :   fprintf(stderr, &quot;PANIC: unprotected error in call to Lua API (%s)\n&quot;,</span>
<span class="lineNum">     642 </span>            :                    lua_tostring(L, -1));
<span class="lineNum">     643 </span><span class="lineNoCov">          0 :   return 0;</span>
<span class="lineNum">     644 </span>            : }
<span class="lineNum">     645 </span>            : 
<span class="lineNum">     646 </span>            : 
<span class="lineNum">     647 </span><span class="lineCov">         99 : LUALIB_API lua_State *luaL_newstate (void) {</span>
<span class="lineNum">     648 </span><span class="lineCov">         99 :   lua_State *L = lua_newstate(l_alloc, NULL);</span>
<span class="lineNum">     649 </span><span class="lineCov">         99 :   if (L) lua_atpanic(L, &amp;panic);</span>
<span class="lineNum">     650 </span><span class="lineCov">         99 :   return L;</span>
<span class="lineNum">     651 </span>            : }
<span class="lineNum">     652 </span>            : 
</pre>
      </td>
    </tr>
  </table>
  <br>

  <table width="100%" border=0 cellspacing=0 cellpadding=0>
    <tr><td class="ruler"><img src="../glass.png" width=3 height=3 alt=""></td></tr>
    <tr><td class="versionInfo">Generated by: <a href="http://ltp.sourceforge.net/coverage/lcov.php" target="_parent">LCOV version 1.13</a></td></tr>
  </table>
  <br>

</body>
</html>
